package com.hxh.service;

import com.hxh.dao.ProjectConfigRecordDao;
import com.hxh.entity.ProjectConfigRecord;
import com.hxh.entity.input.ProjectConfigRecordInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
@Transactional
@Slf4j
public class ProjectConfigRecordService {
    @Resource
    ProjectConfigRecordDao projectConfigRecordDao;

    public boolean deleteById(Integer projectId) {
        return projectConfigRecordDao.deleteById(projectId) > 0;
    }

    /**
     * 忽略前端重启标志
     * 因为前端检测到代码变动，会自动重新编译，也可以自行放开
     */
    private static final boolean ignoreFrontReboot = false;

    public boolean insert(ProjectConfigRecordInput pojo) {
        return projectConfigRecordDao.insert(pojo) > 0;
    }

    public ProjectConfigRecord selectById(Integer projectId) {
        if (projectId == null) {
            return null;
        }
        return projectConfigRecordDao.selectById(projectId);
    }

    public boolean updateSelective(ProjectConfigRecordInput pojo) {

        int res = projectConfigRecordDao.updateSelective(pojo);
        return res > 0;
    }

    public void rebootSystem(Integer id, String module) {
        if (ignoreFrontReboot) {
            module = module.replaceAll("front", "");
        }
        ProjectConfigRecord projectConfigRecord = selectById(id);
        if (projectConfigRecord == null) {
            ProjectConfigRecordInput pojo = new ProjectConfigRecordInput();
            pojo.setProjectId(id);
            pojo.setRebootFlag(module);
            projectConfigRecordDao.insert(pojo);
        } else {
            if (module.contains("front") && module.contains("back")) {
                ProjectConfigRecordInput pojo = new ProjectConfigRecordInput();
                pojo.setProjectId(id);
                pojo.setRebootFlag(module);
                projectConfigRecordDao.updateSelective(pojo);
            } else {
                projectConfigRecordDao.rebootSystem(id, module);
            }
        }
    }

    public void initSystem(Integer id, String module) {
        ProjectConfigRecord projectConfigRecord = selectById(id);
        if (projectConfigRecord == null) {
            ProjectConfigRecordInput pojo = new ProjectConfigRecordInput();
            pojo.setProjectId(id);
            pojo.setInitFlag(module);
            projectConfigRecordDao.insert(pojo);
        } else {
            if (module.contains("front") && module.contains("back")) {
                ProjectConfigRecordInput pojo = new ProjectConfigRecordInput();
                pojo.setProjectId(id);
                pojo.setInitFlag(module);
                projectConfigRecordDao.updateSelective(pojo);
            } else {
                projectConfigRecordDao.initSystem(id, module);
            }
        }
    }

    public void frontRebuildTables(Integer id, String table) {
        ProjectConfigRecord projectConfigRecord = selectById(id);
        if (projectConfigRecord == null) {
            ProjectConfigRecordInput pojo = new ProjectConfigRecordInput();
            pojo.setProjectId(id);
            pojo.setFrontRebuildTables(table);
            projectConfigRecordDao.insert(pojo);
        } else {
            projectConfigRecordDao.frontRebuildTables(id, table);
        }
    }

    public void backRebuildTables(Integer id, String table) {
        ProjectConfigRecord projectConfigRecord = selectById(id);
        if (projectConfigRecord == null) {
            ProjectConfigRecordInput pojo = new ProjectConfigRecordInput();
            pojo.setProjectId(id);
            pojo.setBackRebuildTables(table);
            projectConfigRecordDao.insert(pojo);
        } else {
            projectConfigRecordDao.backRebuildTables(id, table);
        }
    }

    public void addLocalVersion(Integer id) {
        ProjectConfigRecord projectConfigRecord = selectById(id);
        if (projectConfigRecord == null) {
            ProjectConfigRecordInput pojo = new ProjectConfigRecordInput();
            pojo.setProjectId(id);
            pojo.setLocalVersion(0.02f);
            projectConfigRecordDao.insert(pojo);
        }else {
            projectConfigRecordDao.addLocalVersion(id);
        }
    }

    public List<ProjectConfigRecord> getAllChangedRecord() {
        return projectConfigRecordDao.getAllChangedRecord();
    }

    public void clearBackRebuildFlag(Integer projectId, String tableName) {
        projectConfigRecordDao.clearBackRebuildFlag(projectId,tableName);
    }

    public void clearFrontRebuildFlag(Integer projectId, String tableName) {
        projectConfigRecordDao.clearFrontRebuildFlag(projectId,tableName);
    }

    public void clearAllRebootFlag(Integer projectId) {
        projectConfigRecordDao.clearAllRebootFlag(projectId);
    }

    public void clearAllInitFlag(Integer projectId) {
        projectConfigRecordDao.clearAllInitFlag(projectId);
    }
    public void clearInitFlag(Integer projectId,String module) {
        projectConfigRecordDao.clearInitFlag(projectId,module);
    }
    public void clearAllFrontRebuildFlag(Integer projectId) {
        projectConfigRecordDao.clearAllFrontRebuildFlag(projectId);
    }
    public void clearAllBackRebuildFlag(Integer projectId) {
        projectConfigRecordDao.clearAllBackRebuildFlag(projectId);
    }

    public void clearAllFlag(Integer projectId) {
        projectConfigRecordDao.clearAllFlag(projectId);
    }

}
